package com.fingard.dsp.bank.directbank.glb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class GLB01Balance extends GLB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息

        try {
            WriteBankLogLn("进入余额查询处理类 ");
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                GLB01Header glb01Header = new GLB01Header();
                glb01Header.serviceId = "CBE010101";
                glb01Header.serialNo = p_req7506.reqHeader.transSeqID;
                glb01Header.reqTime = p_req7506.reqHeader.transTime.replaceAll(" ", "").replaceAll("-", "").replaceAll(":", "");
                writeCommonHeader(xmlWriter, glb01Header);
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);//账号
                xmlWriter.writeEndDocument();

                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ebank/head/retCode");
                        String returnMsg = tmpBkReader.getFirstText("/ebank/head/retMessage");
                        if (returnCode != null && "000000".equals(returnCode)) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = returnMsg;
                            tmpRetDetail.actCur = p_req7506.readedDetail.actCur;//币种
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;//银行账号
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;//账号地区区域码

                            tmpRetDetail.currBal = tmpBkReader.getFirstText("/ebank/body/balance");//余额
                            tmpRetDetail.availBal = tmpBkReader.getFirstText("/ebank/body/balanceAvailable");//可用余额
                            tmpRetDetail.frozenBal = tmpBkReader.getFirstText("/ebank/body/balanceFreeze");//冻结余额
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(returnMsg);
                            break;
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                        break;
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetBkStr[1]);
                    break;
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
            WriteBankLogLn("余额查询异常：" + e);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
